package com.reactim.message.service.impl;

import com.reactim.message.controller.ConversationController.CreateConversationRequest;
import com.reactim.message.dto.response.ConversationResponse;
import com.reactim.message.entity.Conversation;
import com.reactim.message.service.ConversationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 会话服务实现
 * 简化实现，实际项目中需要数据库支持
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ConversationServiceImpl implements ConversationService {

    @Override
    public Mono<List<ConversationResponse>> getConversationsByUser(String username) {
        // 简化实现：返回模拟数据
        log.debug("获取用户会话列表: {}", username);
        
        List<ConversationResponse> conversations = new ArrayList<>();
        
        // 添加一个示例会话
        ConversationResponse conversation = ConversationResponse.builder()
                .conversationId("conv_" + UUID.randomUUID().toString())
                .name("示例会话")
                .type("private")
                .participants(List.of(1L, 2L))
                .unreadCount(0)
                .createdAt(LocalDateTime.now().minusDays(1))
                .updatedAt(LocalDateTime.now())
                .build();
        
        conversations.add(conversation);
        
        return Mono.just(conversations);
    }

    @Override
    public Mono<ConversationResponse> getConversationById(String conversationId) {
        // 简化实现：返回模拟数据
        log.debug("获取会话信息: {}", conversationId);
        
        ConversationResponse conversation = ConversationResponse.builder()
                .conversationId(conversationId)
                .name("示例会话")
                .type("private")
                .participants(List.of(1L, 2L))
                .unreadCount(0)
                .createdAt(LocalDateTime.now().minusDays(1))
                .updatedAt(LocalDateTime.now())
                .build();
        
        return Mono.just(conversation);
    }

    @Override
    public Mono<ConversationResponse> createConversation(String username, CreateConversationRequest request) {
        // 简化实现：创建新会话
        log.info("创建会话: {} by {}", request.getName(), username);
        
        ConversationResponse conversation = ConversationResponse.builder()
                .conversationId("conv_" + UUID.randomUUID().toString())
                .name(request.getName())
                .type(request.getType())
                .participants(request.getParticipants())
                .unreadCount(0)
                .createdAt(LocalDateTime.now())
                .updatedAt(LocalDateTime.now())
                .build();
        
        return Mono.just(conversation);
    }

    @Override
    public String generateConversationId(Conversation.ConversationType type, Long groupId) {
        // 简化实现：生成群聊会话ID
        return "conv_group_" + groupId;
    }

    @Override
    public String generateConversationId(Conversation.ConversationType type, Long fromUserId, Long toUserId) {
        // 简化实现：生成私聊会话ID
        // 确保会话ID的唯一性，较小的用户ID在前
        Long userId1 = Math.min(fromUserId, toUserId);
        Long userId2 = Math.max(fromUserId, toUserId);
        return "conv_private_" + userId1 + "_" + userId2;
    }

    @Override
    public Mono<Void> updateLastMessage(String conversationId, String messageId, String messagePreview) {
        // 简化实现：记录日志
        log.debug("更新会话最后消息: conversationId={}, messageId={}, preview={}", 
                conversationId, messageId, messagePreview);
        return Mono.empty();
    }
}